package com.cy.wenxinapi.util;

import com.cy.wenxinapi.vo.InterviewParamsVo;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;


/**
 * @author: c
 * @date: 2024/3/22 14:04
 * @description:
 */

@Component
public class RedisUtils {

    @Resource
    private RedisTemplate redisTemplate;

    public List<InterviewParamsVo> getValuesByPrefix(String prefix) {

        // 获取所有匹配前缀的key
        Set<String> keys = redisTemplate.keys(prefix + "*");

        // 初始化存储结果的列表
        List<InterviewParamsVo> values = new ArrayList<>();

        // 如果存在匹配的keys，则遍历并获取对应的value
        if (keys != null && !keys.isEmpty()) {
            for (String key : keys) {
                InterviewParamsVo vo = (InterviewParamsVo) redisTemplate.opsForValue().get(key);
                values.add(vo);
            }
        }

        return values;
    }
}
